import { Module } from 'Vuex'
import http from '@/http/index'
import { api } from '@/http/api'
import GlobalDataProps from '../types'
import { System, DictTypeDataModel, DictTypeQueryModel } from './types'
import { DirtTypeEm } from '@/store/enums'
const system: Module<System, GlobalDataProps> = {
  namespaced: true,
  state: {
    currentVersions: '1.0.0',
    currentPosition: {
      longitude: '', //经度
      latitude: '' //纬度
    },
    flag: 1 //定位状态  等待1 成功2 失败3
  },
  getters: {},
  mutations: {
    setCurrentVersions: (state, versions: string) => {
      const versionsObj = JSON.parse(versions)
      state.currentVersions = versionsObj.version
    },
    setCurrentPosition: (state, currentPosition: string) => {
      if (currentPosition !== '{}') {
        state.flag = 2
        const currentPositionObj = JSON.parse(currentPosition)
        if (
          state.currentPosition.latitude !== currentPositionObj.latitude ||
          state.currentPosition.longitude !== currentPositionObj.longitude
        ) {
          state.currentPosition.latitude = currentPositionObj.latitude
          state.currentPosition.longitude = currentPositionObj.longitude
        }
      } else {
        state.flag = 3
      }
    },
    setPositionFlag: (state, val: number) => {
      state.flag = val
    }
  },
  actions: {
    // 获取数字字典类型 只获取正常的值
    handleGetDictTypeGetByDictType: async ({ commit }: any, params: DictTypeQueryModel): Promise<any> => {
      const finalResult: any = {}
      try {
        let dictTypeCodeList: string[] = []
        let dictTypeCode = ''
        dictTypeCodeList = params.dictTypeCode.split(',')
        dictTypeCodeList.forEach(item => {
          dictTypeCode += `${DirtTypeEm[item as DirtTypeEm]},`
        })
        params.dictTypeCode = dictTypeCode.slice(0, dictTypeCode.length - 1)

        const result: any = await http({
          url: api.getDictTypeGetByDictType,
          params,
          method: 'get'
        })
        if (result) {
          for (const attr in result) {
            finalResult[attr] = result[attr].filter((itemChild: DictTypeDataModel) => {
              itemChild.text = itemChild.value
              itemChild.value = itemChild.code
              return itemChild.status === 0
            })
          }
        }
      } catch (e) {
        console.log(`ERROR FROM system/handleGetDictTypeGetByDictType: ${e}`)
      }
      return finalResult
    },
    handleGetSysNoticeDataStatistics: async ({ commit }: any, data: any): Promise<any> => {
      try {
        const result = await http({
          url: api.getSysNoticeDataStatistics,
          method: 'post',
          data
        })
        console.log(result, 'nn')
      } catch (e) {
        console.log(`ERROR FROM system/handleGetSysNoticeDataStatistics: ${e}`)
      }
    },
    // 获取标签 分页
    handleSysTagLibraryPage: async ({ commit }, params): Promise<any> => {
      let finalResult = ''
      try {
        const result = await http({
          url: api.sysTagLibraryPage,
          method: 'get',
          params
        })
        finalResult = result
      } catch (e) {
        console.log(`ERROR FROM system/handleSysTagLibraryPage: ${e}`)
      }
      return finalResult
    }
  }
}

export default system
